﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Carto;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 图层树操作类
    /// </summary>
    public class TocHelper
    {
        /// <summary>
        /// 根据地图构建图层树
        /// </summary>
        /// <param name="pMap"></param>
        /// <returns></returns>
        public static TreeNode ConstructLyrTree(IMap pMap)
        {
            TreeNode root = new TreeNode("Layers");
            if (pMap == null || pMap.LayerCount == 0) return root;
            ILayer pLyr;
            for (int i = 0; i < pMap.LayerCount; i++)
            {
                pLyr = pMap.get_Layer(i);
                if (pLyr.Valid == false) continue;
                AddTreeNode(pLyr, ref root);
            }
            return root;
        }
        
        /// <summary>
        /// 添加图层到图层树节点中
        /// </summary>
        /// <param name="pLayer"></param>
        /// <param name="treeNode"></param>
        public static void AddTreeNode(ILayer pLayer, ref TreeNode treeNode)
        {
            if (pLayer is IFeatureLayer)
            {
                TreeNode tnLayer = treeNode.Nodes.Add(pLayer.Name);
                tnLayer.Tag = pLayer as IFeatureLayer;
            }
            else if (pLayer is ICompositeLayer)
            {
                ICompositeLayer pCompositeLyr = pLayer as ICompositeLayer;
                TreeNode tnGroup = treeNode.Nodes.Add(pLayer.Name);
                for (int i = 0; i < pCompositeLyr.Count; i++)
                {
                    ILayer pLyr2 = pCompositeLyr.get_Layer(i);
                    AddTreeNode(pLyr2, ref tnGroup);
                }
            }
        }

        /// <summary>
        /// 获取map中layer集合，构建TreeView
        /// </summary>
        /// <param name="pMap">地图</param>
        /// <param name="pTreeView">要创建的树控件</param>
        public static void TreeViewFromMapLayer(IMap pMap, TreeView pTreeView)
        {
            try
            {
                List<IFeatureLayer> allMapLayer = MapHelper.GetAllLayer<IFeatureLayer>(pMap);
                IFeatureLayer pFeatLyr;
                foreach (IFeatureLayer o in allMapLayer)
                {
                    if (o is FeatureLayer)
                    {
                        pFeatLyr = o as IFeatureLayer;
                        if (pFeatLyr.FeatureClass == null)
                        {
                            continue;
                        }
                        if (pFeatLyr.FeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }
                    if (pFeatLyr.Visible && pFeatLyr.Selectable)
                    {
                        TreeNode pNode = pTreeView.Nodes.Add(o.Name);
                        TreeViewFromMapLayerSub(pNode, o);
                    }
                }
            }
            catch
            {
                throw new NullReferenceException("对象为空！");
            }
        }

        /// <summary>
        /// 创建代表一个图层的树节点
        /// </summary>
        /// <param name="pNode">树节点</param>
        /// <param name="pLayer">图层</param>
        private static void TreeViewFromMapLayerSub(TreeNode pNode, ILayer pLayer)
        {
            if (pLayer is ICompositeLayer)
            {
                ICompositeLayer pCompLyr = pLayer as ICompositeLayer;
                for (int i = 0; i < pCompLyr.Count; i++)
                {
                    ILayer pSubLayer = pCompLyr.get_Layer(i);
                    TreeNode pSubNode = pNode.Nodes.Add(pSubLayer.Name);
                    TreeViewFromMapLayerSub(pSubNode, pSubLayer);
                }
            }
            else
            {
                pNode.Name = pLayer.Name;
            }
        }
    }
}
